<template>
	<view class="container">
		<!-- <navbar title="我的地址"></navbar> -->
		<view class="wrap" v-if='showPage'>
			<!-- v-if='list.length > 0' -->
			<view class="address-wrap" v-if='list.length > 0'>
				<view class="address-item" v-for='(item,index) in list'>
					<view class="address-item-left" @click="types == 1? check_address(item.address_id) : ''">
						<view class="address-top">
							<view class="area">{{item.detail}}</view>
							<view class="detail">{{item.address}}</view>
						</view>
						<view class="address-bottom">
							<view class="name">{{item.contact}}</view>
							<view class="call">{{item.mobile}}</view>
							<view class="default line_center" v-if='item.is_default == 1'>默认</view>
						</view>
					</view>
					<view class="address-item-right">
						<image src='../img/edit.png' @tap='goAddres(1,item)'></image>
						<image src='../img/del.png' @tap='delAdModal(item.address_id)'></image>
					</view>
				</view>
			</view>



			<template v-else>
				<!-- <no-data></no-data> -->
				<view class="no_box">
					<image
						src="https://wanchuyun-img.oss-cn-beijing.aliyuncs.com/guomai/33e8637401c0b5640eec48cea9b4fa96e21afa1b.png"
						mode=""></image>
				</view>
			</template>
		</view>
		<dialog-modal text='确定删除吗？' :showModal='showModal' @cancel='cancel' @sure='delAddress'></dialog-modal>
		<view class="footer-fixed">
			<view class="new line_center" @tap='goAddres(0)'>新增地址</view>
		</view>
	</view>
</template>

<script>
	const app = getApp();
	import dialogModal from '@/components/dialog-modal/dialog-modal.vue'
	import icons from '../../../uni_modules/uni-icons/components/uni-icons/icons';
	export default {
		components: {
			dialogModal,
		},
		data() {
			return {
				showPage: false,
				list: [],
				showModal: false,
				ua_id: '',
				types: ''
			};
		},
		onLoad(opt) {
			this.types = opt.type
		},
		onShow() {
			this.getAddressList()
		},
		methods: {
			// 选择地址
			check_address(id) {
				let pages = getCurrentPages()
				let prevPage = pages[pages.length - 2]
				prevPage.$vm.getValue(id)
				uni.navigateBack({
					delta: 1
				})
			},
			/* 获取地址数据 */
			getAddressList() {
				let that = this;
				uni.showLoading({
					title: '加载中...',
					mask: true
				})
				app.globalRequest({
					api: app.getApi().user.addressList,
				}).then(res => {
					console.log('res', res)
					that.showPage = true;
					if (res.code == 1) {
						that.list = res.data;
					}
				}).catch(err => {

				})
			},
			delAdModal(ua_id) {
				this.showModal = true;
				this.ua_id = ua_id

			},
			delAddress() {
				let that = this;
				uni.showLoading({
					title: '加载中...',
					mask: true
				})
				app.globalRequest({
					api: app.getApi().user.deteleaddress,
					method: "POST",
					data: {
						address_id: that.ua_id
					}
				}).then(res => {
					console.log('res', res)
					if (res.code == 1) {

						that.showModal = false;
						that.list = [];
						that.getAddressList()
					}
				}).catch(err => {

				})
			},
			default_set(id) {
				let that = this;
				app.globalRequest({
					api: app.getApi().user.defaultAddress,
					method: "POST",
					data: {
						address_id: id
					}
				}).then(res => {
					if (res.code == 1) {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
						that.getAddressList()
					}
				}).catch(err => {

				})
			},
			cancel() {
				this.showModal = false;
			},
			goAddres(type, item) {
				if (type == 1) {
					var items = JSON.stringify(item)
					uni.navigateTo({
						url: `/pages/user/handle-address/handle-address?type=${type}&item=${items}`
					})
					return;
				}
				uni.navigateTo({
					url: `/pages/user/handle-address/handle-address?type=${type}`
				})
			}
		}
	}
</script>
<style>
	page {
		height: 100%;
	}
</style>
<style lang="scss" scoped>
	.container {
		width: 100%;
		min-height: 100%;
		background: $dis-con-color;
	}

	.wrap {
		padding-bottom: 200rpx;
	}

	.address-wrap {
		margin: 0 30rpx;
	}

	.address-item {
		padding: 30rpx 0;
		display: flex;
		align-items: center;
		justify-content: space-between;
		border-bottom: 1rpx solid #eee;

		&:last-of-type {
			border-bottom: 0;
		}
	}

	.address-top {
		.area{
			font-size: 22rpx;
			color: #676767;
			line-height: 48rpx;
		}
		.detail {
			font-weight: bold;
			font-size: 28rpx;
			line-height: 48rpx;
		}
	}

	.address-bottom {
		display: flex;
		align-items: center;
		margin-top: 12rpx;
		.name{
			font-weight: bold;
		}
		.call{
			margin-left:24rpx;
		}
		.default {
			width: 70rpx;
			height: 40rpx;
			background: #FFF0E3;
			border-radius: 10rpx;
			color:#F84D00;
			font-size: 22rpx;
			margin-left:16rpx;
		}
	}

	.address-item-right {
		width: 100rpx;
		flex-shrink: 0;
		margin-left: 10rpx;
		display: flex;
		justify-content: space-between;
		image{
			width: 30rpx;
			height: 30rpx;
		}
	}

	.footer-fixed {
		width: 100%;
		height: 98rpx;
		background: $dis-con-color;
		display: flex;
		align-items: center;
		justify-content: center;
		position: fixed;
		left: 0;
		bottom: 0;
		padding-bottom: constant(safe-area-inset-bottom);
		padding-bottom: env(safe-area-inset-bottom);
		box-sizing: content-box;

		.new {
			width: 686rpx;
			height: 84rpx;
			background: $dis-liner-color;
			font-size: 28rpx;
			color: #fff;
			border-radius: 42rpx;
		}
	}

	.no_box {
		width: 250rpx;
		height: 250rpx;
		margin: 0rpx auto 0;
		padding-top: 350rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}
</style>